package com.xbai.goods.controller;

import com.github.pagehelper.PageInfo;
import com.xbai.entity.Result;
import com.xbai.goods.pojo.StockBack;
import com.xbai.goods.service.StockBackService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @desc StockBackController
 * @author jxbai
 * @date 2021/1/28
 */

@RestController
@RequestMapping("/stockBack")
@CrossOrigin
@RequiredArgsConstructor
public class StockBackController {

    private final StockBackService stockBackService;

    /***
     * StockBack分页条件搜索实现
     * @param stockBack {@link StockBack}
     * @param page 页码
     * @param size 页大小
     * @return {@link Result<PageInfo>}
     */
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@RequestBody(required = false) StockBack stockBack, @PathVariable  int page, @PathVariable  int size){
        //调用StockBackService实现分页条件查询StockBack
        PageInfo<StockBack> pageInfo = stockBackService.findPage(stockBack, page, size);
        return new Result<>(pageInfo);
    }

    /***
     * StockBack分页搜索实现
     * @param page 页码
     * @param size 页大小
     * @return {@link Result<PageInfo>}
     */
    @GetMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@PathVariable  int page, @PathVariable  int size){
        //调用StockBackService实现分页查询StockBack
        PageInfo<StockBack> pageInfo = stockBackService.findPage(page, size);
        return new Result<>(pageInfo);
    }

    /***
     * 多条件搜索StockBack数据
     * @param stockBack {@link StockBack}
     * @return {@link Result}
     */
    @PostMapping(value = "/search" )
    public Result<List<StockBack>> findList(@RequestBody(required = false) StockBack stockBack){
        //调用StockBackService实现条件查询StockBack
        List<StockBack> list = stockBackService.findList(stockBack);
        return new Result<>(list);
    }

    /***
     * 根据ID删除StockBack数据
     * @param id 主键id
     * @return {@link Result}
     */
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable String id){
        //调用StockBackService实现根据主键删除
        stockBackService.delete(id);
        return new Result();
    }

    /***
     * 修改StockBack数据
     * @param stockBack {@link StockBack}
     * @param id 主键id
     * @return {@link Result}
     */
    @PutMapping(value="/{id}")
    public Result update(@RequestBody StockBack stockBack, @PathVariable String id){
        //设置主键值
        stockBack.setSkuId(id);
        //调用StockBackService实现修改StockBack
        stockBackService.update(stockBack);
        return new Result();
    }

    /***
     * 新增StockBack数据
     * @param stockBack {@link StockBack}
     * @return {@link Result}
     */
    @PostMapping
    public Result add(@RequestBody StockBack stockBack){
        //调用StockBackService实现添加StockBack
        stockBackService.add(stockBack);
        return new Result();
    }

    /***
     * 根据ID查询StockBack数据
     * @param id 主键id
     * @return {@link Result< StockBack >}
     */
    @GetMapping("/{id}")
    public Result<StockBack> findById(@PathVariable String id){
        //调用StockBackService实现根据主键查询StockBack
        StockBack stockBack = stockBackService.findById(id);
        return new Result<>(stockBack);
    }

    /***
     * 查询StockBack全部数据
     * @return {@link Result}
     */
    @GetMapping
    public Result<List<StockBack>> findAll(){
        //调用StockBackService实现查询所有StockBack
        List<StockBack> list = stockBackService.findAll();
        return new Result<>(list) ;
    }
}
